package org.linlinjava.litemall.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.dto.AdminGoodsAccessoryDTO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoodsAccessory;
import org.linlinjava.litemall.db.domain.LitemallMerchandise;
import org.linlinjava.litemall.db.service.LitemallGoodsAccessoryService;
import org.linlinjava.litemall.db.util.PageTools;
import org.springframework.web.bind.annotation.*;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import java.util.List;
@Api(tags = "商品辅料API")
@RestController
@Slf4j
@RequestMapping("/admin/goodsAccessory")
@RequiredArgsConstructor
public class AdminGoodsAccessoryController {
    private final LitemallGoodsAccessoryService litemallGoodsAccessoryService;

    @ApiOperation("商品辅料-「列表」")
    @GetMapping("/list")
    @LogAnno
    public Response list(@RequestParam(name = "limit",defaultValue = "20")Integer limit,
                         @RequestParam(name = "page",defaultValue = "1")Integer page,
                         @RequestParam(name = "groupName",required = false)String groupName,
                         @RequestParam(name = "goodsName",required = false)String goodsName,
                         @RequestParam(name = "name",required = false)String name){
        IPage<AdminGoodsAccessoryDTO> adminGoodsAccessoryDTOS = litemallGoodsAccessoryService.selectJoinListPage(new Page<>(page,limit),AdminGoodsAccessoryDTO.class, new MPJLambdaWrapper<LitemallGoodsAccessory>()
                .selectAll(LitemallGoodsAccessory.class)
                .selectAs(LitemallGoods::getName, AdminGoodsAccessoryDTO::getGoodsName)
                .selectAs(LitemallMerchandise::getName, AdminGoodsAccessoryDTO::getMerchandiseName)
                .like(StrUtil.isNotBlank(groupName), LitemallGoodsAccessory::getGroupName, groupName)
                .like(StrUtil.isNotBlank(goodsName), LitemallGoods::getName, goodsName)
                .like(StrUtil.isNotBlank(name), LitemallGoodsAccessory::getName, name)
                .eq(LitemallGoodsAccessory::getDeleted,0)
                .leftJoin(LitemallGoods.class, LitemallGoods::getId, LitemallGoodsAccessory::getGoodsId)
                .leftJoin(LitemallMerchandise.class, LitemallMerchandise::getId, LitemallGoodsAccessory::getMerchandiseId)
                .orderByAsc(LitemallGoodsAccessory::getAddTime));
        return ResponseUtil.okPage(adminGoodsAccessoryDTOS);
    }

    @ApiOperation("商品辅料-「删除」")
    @GetMapping("/remove")
    @LogAnno
    public Response remove(@RequestParam("id") Integer id){
        return litemallGoodsAccessoryService.removeById(id)?ResponseUtil.ok():ResponseUtil.fail("删除失败");
    }

    @ApiOperation("商品辅料-「批量删除」")
    @PostMapping("/batchesRemove")
    @LogAnno
    public Response batchesRemove(@RequestBody List<Integer> ids){
        return litemallGoodsAccessoryService.removeBatchByIds(ids)?ResponseUtil.ok():ResponseUtil.fail("删除失败");
    }

}
